package light.head.dao.oa;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import light.head.constant.Parm;
import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.framework.entity.PageBean;
import light.head.framework.utils.DateUtils;
import light.head.model.Task;

@Service
public class TaskDAO extends BaseDAO<Task, Integer>{
	
	
	public RESULT findByMap(List<Integer> ocs,Map<String, String> map, PageBean pb){
		StringBuffer hql = new StringBuffer("FROM Task t WHERE t.oc IN (");
		List<Object> list = new ArrayList<Object>();
		for (Integer i : ocs) {
			hql.append("?,");
			list.add(i);
		}
		hql.deleteCharAt(hql.length() - 1).append(")");
		
		if (null != map) {
			
			String du = map.get(Parm.DUSER);
			String hu = map.get(Parm.HUSER);
			String start = map.get(Parm.START);
			String end = map.get(Parm.END);
			String title = map.get(Parm.TITLE);
			String ru = map.get(Parm.RU);
			String ts = map.get(Parm.TS);
			String cu = map.get(Parm.CU);
			String oc = map.get(Parm.OC);
			if(isNotNull(title)) {
				hql.append(" AND t.title like ?");
				list.add("%" + title + "%");
			}if(isNotNull(ru)) {
				hql.append(" AND t.ru = ?");
				list.add(Integer.parseInt(ru));
			}if(isNotNull(start)) {
				hql.append(" AND t.start >= ?");
				list.add(DateUtils.getDateByStr(start, "yyyy-MM-dd"));
			}if(isNotNull(end)) {
				hql.append(" AND t.end <= ?");
				list.add(DateUtils.getDateByStr(end, "yyyy-MM-dd"));
			}if(isNotNull(du)) {
				hql.append(" AND t.du IN (SELECT u.code FROM UserInfo u WHERE u.realName like ?)");
				list.add("%" + du + "%");
			}if(isNotNull(hu)) {
				hql.append(" AND t.ru IN (SELECT u.code FROM UserInfo u WHERE u.realName like ?)");
				list.add("%" + hu + "%");
			}if(isNotNull(ts)) {
				hql.append(" AND t.ts= ?");
				list.add(Integer.parseInt(ts));
			}if(isNotNull(cu)) {
				hql.append(" AND t.user = ?");
				list.add(Integer.parseInt(cu));
			}if(isNotNull(oc)) {
				hql.append(" AND t.oc = ?");
				list.add(Integer.parseInt(oc));
			}
			
		}
		hql.append(" AND t.deleted = 0 or t.deleted is null ");
		hql.append(" ORDER BY t.code DESC");
		
		if (findToPageBean(hql.toString(), pb, list) == RESULT.SUCCESS)
			return RESULT.SUCCESS;
		
		return RESULT.ERROR;
	}
	
	public RESULT update(Task task) {
		if (null == task || null == task.getCode())
			return RESULT.PARAMETER_ERROR;
		
		Task old = get(task.getCode());
		if (null == old)
			return RESULT.NOT_FIND;
		
		if(isNotNull(task.getContent())) {
			old.setContent(task.getContent());
		}if(isNotNull(task.getTitle())) {
			old.setTitle(task.getTitle());
		}if(isNotNull(task.getRemark())) {
			old.setRemark(task.getRemark());
		}if(null != task.getStart()) {
			old.setStart(task.getStart());
		}if(null != task.getEnd()) {
			old.setEnd(task.getEnd());
		}if(null != task.getDu()) {
			old.setDu(task.getDu());
		}if(null != task.getRu()) {
			old.setRu(task.getRu());
		}if(null != task.getUser()) {
			old.setUser(task.getUser());
		}if(null != task.getTs()) {
			old.setTs(task.getTs());
		}if(null != task.getDeleted()) {
			old.setDeleted(task.getDeleted());
		}if(null != task.getFettle()) {
			old.setFettle(task.getFettle());
		}
		
		RESULT result = upd(old);
		return result;
	}
	public List<Task> findTime(String nt){
		StringBuffer sql = new StringBuffer(" FROM Task r WHERE TO_DAYS( r.rt ) - TO_DAYS(?) <= 0 AND r.fettle = 0");
		List<Object> parm = new ArrayList<Object>();
		parm.add(nt);
		return find(sql.toString(), parm.toArray());
	}
}
